package cn.cttic.order.controller;

import java.util.List;

import cn.cttic.order.api.domain.bo.BaseSaleOrderQueryBo;
import cn.cttic.order.api.domain.bo.OrderQueryBaseBo;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.cttic.common.idempotent.annotation.RepeatSubmit;
import cn.cttic.common.log.annotation.Log;
import cn.cttic.common.web.core.BaseController;
import cn.cttic.common.mybatis.core.page.PageQuery;
import cn.cttic.common.core.domain.R;
import cn.cttic.common.core.validate.AddGroup;
import cn.cttic.common.core.validate.EditGroup;
import cn.cttic.common.log.enums.BusinessType;
import cn.cttic.common.excel.utils.ExcelUtil;
import cn.cttic.order.api.domain.vo.BaseSaleOrderVo;
import cn.cttic.order.api.domain.bo.BaseSaleOrderBo;
import cn.cttic.order.service.IBaseSaleOrderService;
import cn.cttic.common.mybatis.core.page.TableDataInfo;

/**
 * 销售订单
 * 前端访问路由地址为:/order/saleOrder
 *
 * @author Times.Studio
 * @date 2024-09-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/saleOrder")
public class BaseSaleOrderController extends BaseController {

    private final IBaseSaleOrderService baseSaleOrderService;

    /**
     * 查询销售订单列表
     */
    @SaCheckPermission("order:saleOrder:list")
    @GetMapping("/list")
    public TableDataInfo<BaseSaleOrderVo> list(BaseSaleOrderQueryBo bo, PageQuery pageQuery) {
        return baseSaleOrderService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出销售订单列表
     */
    @SaCheckPermission("order:saleOrder:export")
    @Log(title = "销售订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BaseSaleOrderBo bo, HttpServletResponse response) {
        List<BaseSaleOrderVo> list = baseSaleOrderService.queryList(bo);
        ExcelUtil.exportExcel(list, "销售订单", BaseSaleOrderVo.class, response);
    }

    /**
     * 获取销售订单详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("order:saleOrder:query")
    @GetMapping("/{id}")
    public R<BaseSaleOrderVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(baseSaleOrderService.queryById(id));
    }

    /**
     * 新增销售订单
     */
    @SaCheckPermission("order:saleOrder:add")
    @Log(title = "销售订单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BaseSaleOrderBo bo) {
        return toAjax(baseSaleOrderService.insertByBo(bo));
    }

    /**
     * 修改销售订单
     */
    @SaCheckPermission("order:saleOrder:edit")
    @Log(title = "销售订单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BaseSaleOrderBo bo) {
        return toAjax(baseSaleOrderService.updateByBo(bo));
    }

    /**
     * 撤销订单
     */
    @SaCheckPermission("order:saleOrder:edit")
    @Log(title = "撤销订单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/cancel")
    public R<BaseSaleOrderVo> cancel(@Validated @RequestBody OrderQueryBaseBo bo) {
        return R.ok(baseSaleOrderService.cancelOrder(bo.getOrderId()));
    }

    /**
     * 撤销订单
     */
    @SaCheckPermission("order:saleOrder:edit")
    @Log(title = "撤销订单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/resendNoticeMessage")
    public R<BaseSaleOrderVo> resendNoticeMessage(@Validated @RequestBody OrderQueryBaseBo bo) {
        return R.ok(baseSaleOrderService.resendNoticeMessage(bo.getOrderId()));
    }

    /**
     * 删除销售订单
     *
     * @param ids 主键串
     */
    @SaCheckPermission("order:saleOrder:remove")
    @Log(title = "销售订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(baseSaleOrderService.deleteWithValidByIds(List.of(ids), true));
    }
}
